<template>
  <div class="app-container">
    <router-view></router-view>
  </div>
</template>

<script>
import Login from "./components/login/Login.vue";
import {onMounted} from 'vue'
import {useRouter} from 'vue-router'

export default {
  name: 'App',
  setup() {
    const router = useRouter()
    onMounted(() => {
      router.beforeEach((to, from, next) => {
        const token = localStorage.getItem('token')
        const requiresAuth = to.matched.some(record => record.meta.requiresAuth)
        if (requiresAuth) {
          // 检查token是否过期
          const expiresAt = JSON.parse(localStorage.getItem('expiresAt'))
          if (Date.now() < expiresAt) {
            // token未过期，执行需要授权的操作
            next()
          } else {
            // token已过期或不存在，需要重新登录
            next('/Login')
          }
        } else {
          next()
        }
      })
    })
  },
  components: {Login}
}
</script>

<style scoped>
</style>
